﻿/**
@title 多个主营行业方法
@param config
	contactBox//主营行业插入框
	addBut//主营行业添加按钮
	delBut//主营行业删除按钮 
	url//json路径
@demo
*/


function mostlyTrade(config){
	var config = config || {};
	var contactBox = $(config.contactBox || "#selMostlyTrade"),
		addBut = $(config.addBut || "#addMostlyTrade"),
		delBut = $(config.delBut || "#delMostlyTrade"),
		url = config.url || "http://www.shangjia.cn/public/ajax/gbt.html";

	$("body").live('click', function(e){
		if(!($(e.target).closest("#MTaddr")[0] || e.target === addBut[0])){
			$("#MTaddr,#MTaddrIe6").remove()
		};
	});
	$(addBut).click(function(){
		if($("#MTaddr").is(":visible")){return false}
		showLinkage(this,url);
	})
	$(delBut).click(function(){
		$(contactBox).find("option:selected").remove();
	})
}


function getTagsse(p,code,callback,url){
	var anchors = '';
	$.getJSON(url + "?parentId=" + code, function(r){
		if(callback){callback(r)};
		if(!r[0]){return false;};
		$("#MTaddr #MTfh").show();
		p.html("");
		if(!!r){
			$("#tesst").css({display:"block"});
			$.each(r, function(i, n){
				if(n.name.length > 7 ){
					var realname = n.name.substring(0,6)+"..";
					anchors += "<li><a href='javascript:;' class='linkage' title=" + n.name + " code='" + n.id + "'>" + realname + "</a></li>"
				}else{
					anchors += "<li><a href='javascript:;' class='linkage' title=" + n.name + " code='" + n.id + "'>" + n.name + "</a></li>"
				}
			})
			p.append(anchors);
			if ( $.browser.msie && $.browser.version == '6.0' ){
				$("#MTaddrIe6").css({"height" : $("#MTaddr").height()+"px"});
			}
		}else{
		}
	});
};


function showLinkage(obj,url){
	var obj = $(obj);
	var url = url;
	var iptOffSet=obj.offset();
	var iptLeft=iptOffSet.left;
	var iptTop=iptOffSet.top+20;
	var str="<div id='MTaddr' class='MTaddr'><span>请选择行业分类<a id='MTfh'>返回顶级列表</a><a id='MTgb'>[&nbsp;关闭&nbsp;]</a></span><ul id='tesst'></ul><div style='clear:both;'></div></div>";
	$("body").append(str);
	$("#tesst").css({display:"none"});
	$("#MTaddr").css({left:iptLeft+"px",top:iptTop+"px"});

	if ( $.browser.msie && $.browser.version == '6.0' ){
		$("body").append("<iframe id='MTaddrIe6' style='width:400px; height:"+$('#MTaddr').height()+"px; left:"+iptLeft+"px; top:"+iptTop+"px; z-index:1;position:absolute;'>Hello</iframe>");
	}
	getTagsse($("#MTaddr ul"),'',function(){
		$("#MTaddr #MTfh").hide();
	},url);
	$("#MTaddr #MTgb").unbind("click").bind("click",function(){
		$("#MTaddr, #MTaddrIe6").remove();
	});
	$("#MTaddr .linkage").die("click").live("click",function(){
		var $t = $(this);
		getTagsse($("#MTaddr ul"),$t.attr("code"),function(r){
			if (r.length < 1) {
				var tempflag = true;
				$("#selMostlyTrade option").each(function(){
					if($(this).val() == $t.attr("code")){
						alert(" 不能重复添加相同的主营行业！");
						tempflag = false;
						return tempflag;
					}
				});
				if(tempflag){
					if($("#selMostlyTrade option").length >= 6){
						$("#MTaddr, #MTaddrIe6").remove();
						alert("最多只能添加6个！");
						return false;
					}
					$("#selMostlyTrade").append("<option selected='selected' value='"+$t.attr("code")+"'>"+$t.attr('title')+"</option>");
					return false;
				}
			}
			return false;
		},url);
	});
	
	$("#MTaddr #MTfh").die("click").live("click",function(){
		getTagsse($("#MTaddr ul"),'',function(){
			$("#MTaddr #MTfh").hide();
		},url);
	});
	return false;
};


//获得相对路径
$.extend({
	includePath:"",
    include: function(file){
        var files = typeof file == "string" ? [file] : file;
        for (var i = 0; i < files.length; i++){
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
            if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");
        }
    },
    includeJsPath: function(file){
		var scripts = document.getElementsByTagName('script');  
		var currentScript = scripts[scripts.length - 1];  
		var currentSrc = currentScript.getAttribute("src");
		var currentSrcLen = currentSrc.lastIndexOf("/");
		var src = currentSrc.substr(0,currentSrcLen+1);
		$.includePath = src;
		$.include(file);
	}
});
$.includeJsPath("images/mostlyTrade.css");